package com.ws.generation.web;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.json.JSONUtil;
import com.ws.common.base.entity.Columns;
import com.ws.common.base.entity.JavaConfig;
import com.ws.common.base.service.IColumnsService;
import com.ws.common.util.SystemUtil;
import com.ws.generation.entity.Config;
import com.ws.generation.entity.GenerationConfig;
import com.ws.generation.util.CodeParsingEngineUtil;
import com.ws.generation.util.FileUtils;
import com.ws.generation.util.GenerationUtils;
import com.ws.generation.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/generation")
public class GenerationWeb {


    
    @Autowired
    IColumnsService columnsService;
    
    @RequestMapping("/getColumns")
    @ResponseBody
    public Object getColumns(){
        //读取配置文件
        String configString= FileUtils.readString("doc/config.txt");
        Config config=FileUtils.getConfig(configString);
        
        //读取表的列信息
    	List<Columns> columns=columnsService.selectColumnsByTableName(config.getTableName());
        
        //获取字符串
        String entityString=FileUtils.readString("doc/template/User.java");
        String daoString=FileUtils.readString("doc/template/UserMapper.java");
        String serviceString=FileUtils.readString("doc/template/UserService.java");
        String serviceImplString=FileUtils.readString("doc/template/UserServiceImpl.java");
        String utilString=FileUtils.readString("doc/template/UserUtil.java");
        String webString=FileUtils.readString("doc/template/UserWeb.java");
        
        //替换模版
        entityString= StringUtils.myReplace(entityString,config);
        daoString=StringUtils.myReplace(daoString,config);
        serviceString=StringUtils.myReplace(serviceString,config);
        serviceImplString=StringUtils.myReplace(serviceImplString,config);
        utilString=StringUtils.myReplace(utilString,config);
        webString=StringUtils.myReplace(webString,config);
        

    	//util 工具生成
    	String tableFilter=StringUtils.getStringFilter(columns);
    	//字段生成
    	String tableFiled=StringUtils.getTableFiled(columns);
    	//get set工具
    	String tableGetAndSet=StringUtils.getTableGetAndSet(columns);
    	String tableID=StringUtils.getTableId(columns);
        //字符过滤
        utilString=StringUtils.myReplaceTableFilter(utilString,tableFilter);
        //替换
        entityString=StringUtils.myReplaceEntityString(entityString,tableFiled,tableGetAndSet,tableID);
        
        //路径
        String entityDir="doc/out/"+config.getDirName()+"/entity";
        String entityFileName=config.getUpperJavaTableName()+".java";
        
        String mapperDir="doc/out/"+config.getDirName()+"/mapper";
        String mapperFileName=config.getUpperJavaTableName()+"Mapper.java";
        
        String serviceDir="doc/out/"+config.getDirName()+"/service";
        String serviceFileName=config.getUpperJavaTableName()+"Service.java";
        
        String serviceImplDir="doc/out/"+config.getDirName()+"/service/impl";
        String serviceImplFileName=config.getUpperJavaTableName()+"ServiceImpl.java";

        String utilImplDir="doc/out/"+config.getDirName()+"/util";
        String utilFileName=config.getUpperJavaTableName()+"Util.java";

        String webDir="doc/out/"+config.getDirName()+"/web";
        String webFileName=config.getUpperJavaTableName()+"Web.java";
        
        FileUtils.writeString(entityString,entityDir,entityFileName);
        FileUtils.writeString(daoString,mapperDir,mapperFileName);
        FileUtils.writeString(serviceString,serviceDir,serviceFileName);
        FileUtils.writeString(serviceImplString,serviceImplDir,serviceImplFileName);
        FileUtils.writeString(utilString,utilImplDir,utilFileName);
        FileUtils.writeString(webString,webDir,webFileName);
        System.out.println("成功！！！！！！");
        return columns;
    }

    @RequestMapping("/regexTest")
    @ResponseBody
    public Object regexTest(){
        String s="";
        s+="<for column:columns>\n";
        s+="   ${column.columnName}  \n";
        s+="   ${column.columnKey}  \n";
        s+="</for>";
        Pattern pattern=Pattern.compile("<for.*?column.*?:columns>([\\s\\S]*?)</for>");
        Matcher matcher=pattern.matcher(s);
        System.out.println(matcher.find());
        System.out.println(matcher.group(0));
        System.out.println(matcher.group(1));
        System.out.println(matcher.start());
        System.out.println(matcher.end());
        return s;
    }

    @RequestMapping("/sc")
    @ResponseBody
    public Object sc(){
        String baseDir=System.getProperty("user.dir")+"/generation";
        String dir=baseDir+"/doc/config.txt";
        //读取配置文件
        String configString= new FileReader(dir).readString();
        JavaConfig config= SystemUtil.getConfig(configString);

        //读取表的列信息
        List<Columns> columns=columnsService.selectColumnsByTableName(config.getTableName());

        //获取字符串
        String entityString=new FileReader(baseDir+"/doc/template/User.java").readString();
        String daoString=new FileReader(baseDir+"/doc/template/UserMapper.java").readString();
        String iServiceString=new FileReader(baseDir+"/doc/template/IUserService.java").readString();
        String serviceImplString=new FileReader(baseDir+"/doc/template/UserServiceImpl.java").readString();
        String webString=new FileReader(baseDir+"/doc/template/UserWeb.java").readString();
        String mapperString=new FileReader(baseDir+"/doc/template/UserMapper.xml").readString();
        String listHtmlString=new FileReader(baseDir+"/doc/template/list.html").readString();
        String editHtmlString=new FileReader(baseDir+"/doc/template/edit.html").readString();

        //替换模版
        entityString= CodeParsingEngineUtil.sc(entityString,config,columns);
        daoString= CodeParsingEngineUtil.sc(daoString,config,columns);
        iServiceString= CodeParsingEngineUtil.sc(iServiceString,config,columns);
        serviceImplString= CodeParsingEngineUtil.sc(serviceImplString,config,columns);
        webString= CodeParsingEngineUtil.sc(webString,config,columns);
        mapperString= CodeParsingEngineUtil.sc(mapperString,config,columns);
        listHtmlString= CodeParsingEngineUtil.sc(listHtmlString,config,columns);
        editHtmlString= CodeParsingEngineUtil.sc(editHtmlString,config,columns);

        //输出到文件
        new FileWriter(baseDir+"/doc/out/"+config.getPackageDir()+"/entity/"+config.getJavaName()+".java").write(entityString);
        new FileWriter(baseDir+"/doc/out/"+config.getPackageDir()+"/dao/"+config.getJavaName()+"Mapper.java").write(daoString);
        new FileWriter(baseDir+"/doc/out/"+config.getPackageDir()+"/service/I"+config.getJavaName()+"Service.java").write(iServiceString);
        new FileWriter(baseDir+"/doc/out/"+config.getPackageDir()+"/service/impl/"+config.getJavaName()+"ServiceImpl.java").write(serviceImplString);
        new FileWriter(baseDir+"/doc/out/"+config.getPackageDir()+"/web/"+config.getJavaName()+"Web.java").write(webString);
        new FileWriter(baseDir+"/doc/out/"+config.getPackageDir()+"/dao/"+config.getJavaName()+"Mapper.xml").write(mapperString);
        new FileWriter(baseDir+"/doc/out/"+config.getControllerDir()+"/list.html").write(listHtmlString);
        new FileWriter(baseDir+"/doc/out/"+config.getControllerDir()+"/edit.html").write(editHtmlString);
        String s=entityString+"\n"+daoString+"\n"+iServiceString+"\n"+webString+"\n"+serviceImplString+"\n"+mapperString;
        System.out.println(s);
        return "成功！";
    }

    @RequestMapping("/adminSc")
    @ResponseBody
    public Object adminSc(HttpServletRequest request,String configJson){
        GenerationConfig config=JSONUtil.toBean(configJson,GenerationConfig.class);
        GenerationUtils.setDefaultConfig(config);

        //读取表的列信息
        List<Columns> columns=columnsService.selectColumnsByTableName(config.getTableName());
        String  systemDir=System.getProperty("user.dir")+"/generation";
        String  baseDir=config.getGeneratorDir();

        //获取字符串
        String entityString=new FileReader(systemDir+"/doc/template/User.java").readString();
        String daoString=new FileReader(systemDir+"/doc/template/UserMapper.java").readString();
        String iServiceString=new FileReader(systemDir+"/doc/template/IUserService.java").readString();
        String serviceImplString=new FileReader(systemDir+"/doc/template/UserServiceImpl.java").readString();
        String webString=new FileReader(systemDir+"/doc/template/UserWeb.java").readString();
        String mapperString=new FileReader(systemDir+"/doc/template/UserMapper.xml").readString();
        String listHtmlString=new FileReader(systemDir+"/doc/template/list.html").readString();
        String editHtmlString=new FileReader(systemDir+"/doc/template/edit.html").readString();

        //替换模版
        entityString= CodeParsingEngineUtil.scAdmin(entityString,config,columns);
        daoString= CodeParsingEngineUtil.scAdmin(daoString,config,columns);
        iServiceString= CodeParsingEngineUtil.scAdmin(iServiceString,config,columns);
        serviceImplString= CodeParsingEngineUtil.scAdmin(serviceImplString,config,columns);
        webString= CodeParsingEngineUtil.scAdmin(webString,config,columns);
        mapperString= CodeParsingEngineUtil.scAdmin(mapperString,config,columns);
        listHtmlString= CodeParsingEngineUtil.scAdmin(listHtmlString,config,columns);
        editHtmlString= CodeParsingEngineUtil.scAdmin(editHtmlString,config,columns);

        //输出到文件
        new FileWriter(baseDir+"/"+config.getPackageDir()+"/entity/"+config.getJavaName()+".java").write(entityString);
        new FileWriter(baseDir+"/"+config.getPackageDir()+"/dao/"+config.getJavaName()+"Mapper.java").write(daoString);
        new FileWriter(baseDir+"/"+config.getPackageDir()+"/service/I"+config.getJavaName()+"Service.java").write(iServiceString);
        new FileWriter(baseDir+"/"+config.getPackageDir()+"/service/impl/"+config.getJavaName()+"ServiceImpl.java").write(serviceImplString);
        new FileWriter(baseDir+"/"+config.getPackageDir()+"/web/"+config.getJavaName()+"Web.java").write(webString);
        new FileWriter(baseDir+"/"+config.getPackageDir()+"/dao/"+config.getJavaName()+"Mapper.xml").write(mapperString);
        new FileWriter(baseDir+config.getControllerDir()+"/list.html").write(listHtmlString);
        new FileWriter(baseDir+config.getControllerDir()+"/edit.html").write(editHtmlString);
        String s=entityString+"\n"+daoString+"\n"+iServiceString+"\n"+webString+"\n"+serviceImplString+"\n"+mapperString;
        return "生成成功！！";
    }
}
